package com.xiaoge.libraryuse.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.xiaoge.basedevlibrary.imageload.ImageUtils;
import com.xiaoge.libraryuse.R;
import com.xiaoge.libraryuse.bean.MoreTypeBean;

import java.util.List;

/**
 * Created by guoyzh on 2018/3/9 0009.
 */

public class MuliListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    //定义三种常量  表示三种条目类型
    public static final int TYPE_PULL_IMAGE = 0;
    public static final int TYPE_RIGHT_IMAGE = 1;
    public static final int TYPE_THREE_IMAGE = 2;
    private List<MoreTypeBean> mData;

    public MuliListAdapter(List data) {
        this.mData = data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //创建不同的 ViewHolder
        View view;
        //根据viewtype来创建条目
        if (viewType == TYPE_PULL_IMAGE) {
            view = View.inflate(parent.getContext(), R.layout.item_pull_img, null);
            return new PullImageHolder(view);
        } else if (viewType == TYPE_RIGHT_IMAGE) {
            view = View.inflate(parent.getContext(), R.layout.item_right_img, null);
            return new RightImageHolder(view);
        } else {
            view = View.inflate(parent.getContext(), R.layout.item_three_img, null);
            return new ThreeImageHolder(view);
        }
    }

    /**
     * 填充每个条目的内容
     *
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        if (holder instanceof PullImageHolder) {

            // 设置大图片模式的显示内容
            // ((PullImageHolder) holder).tv.setText("");
            // ((PullImageHolder) holder).iv.setBackgroundResource(R.mipmap.i1);
            ImageUtils.getInstance().load(((PullImageHolder) holder).iv, "https://ws1.sinaimg.cn/large/610dc034gy1fi678xgq1ij20pa0vlgo4.jpg");

        } else if (holder instanceof RightImageHolder) {

            // 设置图片居右侧的展示内容
            // ((RightImageHolder) holder).tv.setText("");
            // ((RightImageHolder) holder).iv.setBackgroundResource(R.mipmap.i2);
            ImageUtils.getInstance().load(((RightImageHolder) holder).iv, "https://ws1.sinaimg.cn/large/610dc034gy1fi678xgq1ij20pa0vlgo4.jpg");

        } else if (holder instanceof ThreeImageHolder) {

            // 设置图片居于底部时候的展示内容
            // ((ThreeImageHolder) holder).tv.setText("");
            // ((ThreeImageHolder) holder).iv1.setBackgroundResource(R.mipmap.i3);
            // ((ThreeImageHolder) holder).iv2.setBackgroundResource(R.mipmap.i4);
            // ((ThreeImageHolder) holder).iv3.setBackgroundResource(R.mipmap.i5);
            ImageUtils.getInstance().load(((ThreeImageHolder) holder).iv1, "https://ws1.sinaimg.cn/large/610dc034gy1fi678xgq1ij20pa0vlgo4.jpg");
            ImageUtils.getInstance().load(((ThreeImageHolder) holder).iv2, "https://ws1.sinaimg.cn/large/610dc034gy1fi678xgq1ij20pa0vlgo4.jpg");
            ImageUtils.getInstance().load(((ThreeImageHolder) holder).iv3, "https://ws1.sinaimg.cn/large/610dc034gy1fi678xgq1ij20pa0vlgo4.jpg");

        }
    }

    @Override
    public int getItemCount() {
        if (mData != null) {
            return mData.size();
        }
        return 0;
    }


    /**
     * 根据条件返回条目的类型 不重写这个方法会导致所有的条目都是同一种布局
     *
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        MoreTypeBean moreTypeBean = mData.get(position);
        if (moreTypeBean.type == 0) {
            return TYPE_PULL_IMAGE;
        } else if (moreTypeBean.type == 1) {
            return TYPE_RIGHT_IMAGE;
        } else {
            return TYPE_THREE_IMAGE;
        }
    }

    /**
     * 创建三种ViewHolder
     */
    private class PullImageHolder extends RecyclerView.ViewHolder {
        TextView tv;
        ImageView iv;

        public PullImageHolder(View itemView) {
            super(itemView);
            tv = (TextView) itemView.findViewById(R.id.tv);
            iv = (ImageView) itemView.findViewById(R.id.iv);
        }
    }

    private class RightImageHolder extends RecyclerView.ViewHolder {
        TextView tv;
        ImageView iv;

        public RightImageHolder(View itemView) {
            super(itemView);
            tv = (TextView) itemView.findViewById(R.id.tv);
            iv = (ImageView) itemView.findViewById(R.id.iv);
        }
    }

    private class ThreeImageHolder extends RecyclerView.ViewHolder {
        TextView tv;
        ImageView iv1;
        ImageView iv2;
        ImageView iv3;

        public ThreeImageHolder(View itemView) {
            super(itemView);
            tv = (TextView) itemView.findViewById(R.id.tv);
            iv1 = (ImageView) itemView.findViewById(R.id.iv1);
            iv2 = (ImageView) itemView.findViewById(R.id.iv2);
            iv3 = (ImageView) itemView.findViewById(R.id.iv3);
        }
    }
}
